package org.liveboardgames.agricola.control.delegates.action.processor
{
    import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
    import org.liveboardgames.agricola.control.events.intern.ActionEvent;
    import org.liveboardgames.agricola.model.enums.ResourceTypeEnum;
    import org.liveboardgames.agricola.vo.action.Action;
    import org.liveboardgames.agricola.vo.card.Card;
    import org.liveboardgames.agricola.vo.common.Target;
    import org.liveboardgames.agricola.vo.player.Player;
    import org.liveboardgames.common.dictionary.collections.IMap;
    
    public class ResourceActionProcessor extends ActionProcessor
    {
        private var _resourceType:String;
        
        private var _outputQuantity:int;
        
        public function ResourceActionProcessor(action:Action, resourceType:String, outputQuantity:int)
        {
            super(action);
            this._resourceType = resourceType;
            this._outputQuantity = outputQuantity;
        }
        
        override public function execute():void
        {
            var target:Target = action.target
            if (target is Card)
            {
                var card:Card = target as Card;
                card.addResource(_resourceType, _outputQuantity);
                this.dispatchEvent(new ActionEvent(ActionEvent.ACTION_COMPLETE));
            }
            else if (target is Player)
            {
                // This was introduced to handle the Animals card for 5 players, but design looks strange
                var player:Player = target as Player;
                var cost:IMap = action.cost;
                GameHelper.pay(player.farm, cost);
                
                if (ResourceTypeEnum.ANIMALS.contains(_resourceType))
                {
                    new AnimalActionProcessor(action, _resourceType, _outputQuantity).execute();
                }
                else
                {
                    player.farm.addResource(_resourceType, _outputQuantity);
                    this.dispatchEvent(new ActionEvent(ActionEvent.ACTION_COMPLETE));
                }
            }
            else
            {
                throw new Error("not supported target " + target + " for type " + _resourceType);
            }
        }
    
    }
}